package rock.negagame.client.sprites;

import static com.google.gwt.event.dom.client.KeyCodes.KEY_DOWN;
import static com.google.gwt.event.dom.client.KeyCodes.KEY_LEFT;
import static com.google.gwt.event.dom.client.KeyCodes.KEY_RIGHT;
import static com.google.gwt.event.dom.client.KeyCodes.KEY_UP;

import java.util.Vector;

import rock.negagame.client.sprites.control.GameController;
import rock.negagame.client.sprites.control.Pantalla;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Image;

public class Jugador extends Sprite{

	Timer t;
	int codigo = 0;
	static int DESP_JUG=9;
	Vector<Bala> balas;
	
	public Jugador(Image img) {
		super(img);
		// TODO Auto-generated constructor stub
		vida = 3;
		posX = (Pantalla.w/2) - 50;
		posY = Pantalla.h-170;
		balas = new Vector<Bala>();
		iniciaBalas();
		
		t = new Timer(){
		      @Override
		      public void run() {
		        mueve(codigo);		        
		      }
		    };
	}
	private void iniciaBalas(){
		for(int i=0; i<5; i++){
			Bala bala = new Bala(new Image("pua.png"),true);
			balas.add(bala);
			Pantalla.getPantalla().add(bala);
			bala.setPosition(1, 1);
		}
	}
	public int getWidth(){
		return img.getOffsetWidth()-25;
	}
	public int getHeight(){
		return img.getOffsetHeight()-25;
	}
	public boolean restaVida(){
		if(vida == 0){
			return false;
		}else{
			vida--;
			
			return true;
		}
	}
	public void para(int code){
		if(code == KEY_UP || code == KEY_DOWN||code == KEY_LEFT||code == KEY_RIGHT)
		t.cancel();
	}
	public void mueve(int code){
		if(explotar){
			
		}else{
			switch(codigo){
	        case KEY_UP:	moveUp(DESP_JUG);
			
			break;
		case KEY_DOWN:	moveDown(DESP_JUG);
			break;
		case KEY_LEFT:	moveLeft(DESP_JUG);
			break;
		case KEY_RIGHT:	moveRigth(DESP_JUG);
			break;
		default:
			break;
	        }
		}
		
	}
	public void empieza(int code){
		this.codigo = code;
		mueve(code);
		t.scheduleRepeating(40);
	}
	public void dispara(){
		GameController.getGame().l.setText(GameController.getGame().l.getText() + " a disparar2.........  ");
		boolean encontrado = false;
		for(int i=0; i<balas.size()&&!encontrado; i++){
			Bala b = balas.get(i);
			if(!b.activa){
				b.disparo(this);
				encontrado = true;
				GameController.getGame().l.setText(GameController.getGame().l.getText() + " disparo numero "+ i);
			}
		}
	}
	/**
	 * @return the balas
	 */
	public Vector<Bala> getBalas() {
		return balas;
	}
	/**
	 * @param balas the balas to set
	 */
	public void setBalas(Vector<Bala> balas) {
		this.balas = balas;
	}

}
